package mi.payment.ui.print.helper;

import android.text.TextUtils;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import mi.payment.controller.BaseController;
import mi.payment.controller.SettlementCtrl;
import mi.payment.db.dao.PayDetail;
import mi.payment.settings.LastBatchTransDataOperation;
import mi.payment.settings.SalesSlipPrintSettingsOperation;
import mi.payment.settings.TransactionOtherOperation;
import mi.payment.ui.base.ParentActivity;
import mi.payment.ui.print.view.CardDetailsView;
import mi.payment.ui.print.view.PrintCardView1;
import mi.payment.utils.CollectionUtil;
import mi.payment.utils.iso8583.Utils;
import mi.payment.utils.print.DataPrinterKernel;
import woyou.aidlservice.jiuiv5.ICallback;


public class BasePrintController extends BaseController {

    protected DataPrinterKernel dataPrinterKernel; // 打印
    protected boolean lastBatch; // 是否是前一批次
    protected TotalSettlementDetail totalSettlementDetail; // 交易总计
    protected List<PayDetail> transactionDetails; // 交易明细列

    public BasePrintController() {
        this(null);
    }

    public BasePrintController(ParentActivity activity) {
        this(activity, false);
    }

    public BasePrintController(ParentActivity activity, boolean lastBatch) {
        super(activity);
        this.lastBatch = lastBatch;
        dataPrinterKernel = DataPrinterKernel.INSTANCE();
    }

    /**
     * 初始化交易总计和交易明细列表
     */
    public void initPrintData() {
        if (lastBatch) {
            totalSettlementDetail = LastBatchTransDataOperation.getInstance().getLastBatchTotalSettlementDetail();
            transactionDetails = LastBatchTransDataOperation.getInstance().getLastBatchTransDetails();
        } else {
            totalSettlementDetail = SettlementCtrl.calSettlementDetail();
            transactionDetails = SettlementCtrl.calTransDetailsList();
        }
    }

    /**
     * 获取打印交易明细的详情view
     */
    public List<View> getTransactionDetailView() {
        List<View> result = new ArrayList<>();
        if (!CollectionUtil.isEmpty(transactionDetails)) {
            CardDetailsView cardDetailsView = new CardDetailsView(activity);
            cardDetailsView.setData(transactionDetails);
            result.add(cardDetailsView);
        } else {
            result.add(new CardDetailsView(activity));
        }
        return result;
    }

    public void setLastBatch(boolean lastBatch) {
        this.lastBatch = lastBatch;
    }

    public boolean getLastBatch() {
        return lastBatch;
    }

    public TotalSettlementDetail getTotalSettlementDetail() {
        return totalSettlementDetail;
    }

    public List<PayDetail> getTransactionDetails() {
        return transactionDetails;
    }

    /**
     * 打印交易总计
     */
    public void printTotalSettlementDetail(ICallback callback) throws Exception {
        dataPrinterKernel.printTotalSettlement(totalSettlementDetail, callback);
    }

    /**
     * 打印交易明细
     */
    public void printTransactionDetail(ICallback callback) {
        printTransactionDetail(transactionDetails, callback);
    }

    /**
     * 打印交易明细
     */
    public void printTransactionDetail(List<PayDetail> transactionDetails, ICallback callback) {
        // 2017/09/21 修复空结算打印不回调的问题。
        dataPrinterKernel.transactionDetail(transactionDetails, callback);
    }

    public View getPreviewView(PayDetail payDetail) {
        if (payDetail == null) {
            return new View(activity);
        }
        // 0：银行卡 1：支付宝 2:微信 3:口碑 4:银联钱包 5:小米钱包
        int qrCodeTransPlatform = payDetail.getTransPlatform();
        switch (qrCodeTransPlatform) {
            case 0:
                PrintCardView1 cardView1 = new PrintCardView1(activity);
                cardView1.show(payDetail);
                return cardView1;
            default:
                return new CodeViewController(activity).getPrintView(payDetail);
        }
    }

    public void print(PayDetail payDetail, ICallback callback, int pageNum, boolean isAgain) throws Exception {
        // 0：银行卡 1：支付宝 2:微信 3:口碑 4:银联钱包
        int transPlatform = payDetail.getTransPlatform();
        switch (transPlatform) {
            case 0:
                dataPrinterKernel.printBankTicket(payDetail, callback, pageNum, isAgain);
                break;
            default:
                dataPrinterKernel.printQRCodeTicket(payDetail, isAgain, callback, pageNum, true);
                break;
        }
    }

    /**
     * 生成电子签名特征码
     */
    public String generateTransFeatureCode(PayDetail payDetail) {
        // 清算日期
        String settleDate = payDetail.getSettleDate();
        if (TextUtils.isEmpty(settleDate)) {
            settleDate = "0000";
        }
        // 参考号
        String referNo = payDetail.getReferNo();

        String blockHex = settleDate + referNo;
        if (blockHex.length() == 16) {
            byte[] block = Utils.hexStr2Bytes(blockHex);
            byte[] result = new byte[4];
            byte[] block1 = new byte[4];
            byte[] block2 = new byte[4];
            System.arraycopy(block, 0, block1, 0, 4);
            System.arraycopy(block, 4, block2, 0, 4);
            for (int i = 0; i < block1.length; i++) {
                result[i] = (byte) (block1[i] ^ block2[i]);
            }
            return Utils.byte2HexStr(result);
        }
        return null;
    }

    /**
     * 打印完成后是否返回主页面
     */
    public boolean returnHomeAfterPrint() {
        return false;
    }

    /**
     * 自定义小票打印内容
     **/
    public String getSmallTicketTail() {
        return "";
    }

    /**
     * 是否打印畅捷版本信息
     **/
    public boolean isPrintCpVersion() {
        return false;
    }

    /**
     * 是否打印小票需要打印持卡人姓名+借贷记卡标示
     */
    public boolean isPrintCardHoldNameAndCardType() {
        return false;
    }

    /**
     * 结算时是否显示结算明细对话框
     */
    public boolean isShowSettlementDetailDlg() {
        return true;
    }

    /**
     * 是否支持电子签名
     */
    public boolean isSupportESignature() {
        return TransactionOtherOperation.getInstance().getTransactionOtherSettings().isSupportESignature();
    }

    /**
     * 是否打印副标题
     */
    public boolean isPrintSubtitle() {
        return SalesSlipPrintSettingsOperation.getInstance().getSalesSlipPrintSettings().isPrintSubtitle();
    }

    public boolean isShowLastLastBatch() {
        return false;
    }

    /**
     * 撤销、预授权完成、交易  是否需打印原凭证号
     */
    public boolean isPrintOriginalPOSNum() {
        return false;
    }

    /**
     * 退货、预授权撤销、预授权完成撤销需打印原参考号信息
     */
    public boolean isPrintOriginalReferNo() {
        return false;
    }

}
